package com.fld.fldcodemother.core;

import com.fld.fldcodemother.ai.AiCodeGeneratorService;
import com.fld.fldcodemother.ai.model.HtmlCodeResult;
import com.fld.fldcodemother.ai.model.MultiFileCodeResult;
import com.fld.fldcodemother.exception.BusinessException;
import com.fld.fldcodemother.exception.ErrorCode;
import com.fld.fldcodemother.model.enums.CodeGenTypeEnum;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.io.File;


/**
 *  门面模式
 *  门面模式通过提供一个统一的高层接口来隐藏子系统的复杂性，
 *  让客户端只需要与这个简化的接口交互，而不用了解内部的复杂细节实现
 */
@Service
public class AiCodeGeneratorFacade {

    @Resource
    private AiCodeGeneratorService aiCodeGeneratorService;

    public File generateAndSaveCode (String userMessage, CodeGenTypeEnum codeGenTypeEnum) {
        // 判断生成类型参数是否为空
        if (codeGenTypeEnum == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"生成类型为空");
        }

        // 根据生成类型参数判断生成什么类型的文件
        return switch (codeGenTypeEnum)
        {
            case HTML -> generateAndSaveHtmlCode(userMessage);

            case MULTI_FILE -> generateAndSaveMultiFileCode(userMessage);

            default -> {
                String errorMessage = "不支持的生成类型" + codeGenTypeEnum.getValue();
                throw new BusinessException(ErrorCode.SYSTEM_ERROR,errorMessage);
            }
        };
    }

    /**
     * 要求生成html File
     * @param userMessage 用户提示词
     * @return 返回代码文件
     */
    public File generateAndSaveHtmlCode (String userMessage) {
        HtmlCodeResult htmlCodeResult = aiCodeGeneratorService.generateHtmlCode(userMessage);
        return CodeFileSaver.saveHtmlCodeResult(htmlCodeResult);
    }

    /**
     * 要求生成多类型代码 File
     * @param userMessage 用户提示词
     * @return 返回代码文件
     */
    public File generateAndSaveMultiFileCode (String userMessage) {
        MultiFileCodeResult multiFileCodeResult = aiCodeGeneratorService.generateMultiFileCode(userMessage);
        return CodeFileSaver.saveMultiFileCodeResult(multiFileCodeResult);
    }

}
